package com.tutu.service.impl;

import com.tutu.core.Constant;
import com.tutu.core.api.SystemApiUrls;
import com.tutu.service.DeviceService;
import com.tutu.service.MasterService;
import com.tutu.utils.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * MasterServiceImpl
 * Created by 涂奕恒 on 2018/10/19 0019 下午 4:35.
 */
@Service
public class MasterServiceImpl implements MasterService {

    @Autowired
    DeviceService deviceService;

    @Override
    public Map<String, Object> loginIn(HttpServletRequest request, String account, String password) {
        Map<String, Object> map = HttpRequestUtil.getOnlyData(SystemApiUrls.base_prefix + SystemApiUrls.master_login, "account=" + account + "&password=" + Md5Util.md5(password));

        if (map.containsKey("id")) {
            request.getSession().setAttribute(Constant.CURRENT_LOGIN_MASTER, map);
            request.getSession().setAttribute(Constant.CURRENT_LOGIN_MASTER_ID, map.get("id"));
            request.getSession().setAttribute(Constant.CURRENT_LOGIN_MASTER_NAME, map.get("name"));
            request.getSession().setAttribute(Constant.CURRENT_LOGIN_COMPANY_ID, StringUtils.isBlank(map.get("company") + "") ? null : JsonUtil.json2Map(map.get("company")).get("id"));
            request.getSession().setAttribute(Constant.CURRENT_LOGIN_COMPANY_NAME, StringUtils.isBlank(map.get("company") + "") ? "" : JsonUtil.json2Map(map.get("company")).get("name"));

            // 加载相关联的设备信息
            deviceService.loadCacheData(request);

            LogInfoServiceImpl.saveLogInfo(request, "登录管理后台");

            return map;
        }

        return null;
    }

    @Override
    public void loginOut(HttpServletRequest request) {
        request.getSession().removeAttribute(Constant.CURRENT_LOGIN_MASTER);
    }

    @Override
    public Map<String, Object> modifyPwd(HttpServletRequest request, String newPwd) {
        try {
            LogInfoServiceImpl.saveLogInfo(request, "修改登录密码");

            Integer masterId = WebUtil.getSysInfo(request, Constant.CURRENT_LOGIN_MASTER_ID);

            if (null != masterId) return HttpRequestUtil.getCompleteData(SystemApiUrls.base_prefix + SystemApiUrls.master_modifyPwd, "id=" + masterId + "&password=" + newPwd);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return BaseUtil.defaultErrorInfo(null);
    }

    @Override
    public Map<String, Object> pageByParams(Integer companyId, Integer status, String keyWord, Integer pageNum, Integer pageSize) {
        return HttpRequestUtil.getOnlyData(SystemApiUrls.base_prefix + SystemApiUrls.master_page, "status=" + status + "&companyId=" + companyId + "&keyWord=" + UnicodeUtil.unicodeEncode(keyWord) + "&pageNum=" + pageNum + "&pageSize=" + pageSize);
    }

    @Override
    public Map<String, Object> getInfoById(Integer id) {
        return HttpRequestUtil.getCompleteData(SystemApiUrls.base_prefix + SystemApiUrls.master_info, "id=" + id);
    }

    @Override
    public Map<String, Object> saveInfo(HttpServletRequest request, Integer id, Integer roleId, Integer companyId, String account, String name, String password, String mobile, String email, Integer status) {
        LogInfoServiceImpl.saveLogInfo(request, "修改管理员信息，ID：" + id + "，角色ID：" + roleId + "，公司ID：" + companyId + "，账号：" + account + "，名称：" + name + "，联系方式：" + mobile + "，电子邮箱：" + email + "，启用状态：" + status);

        String url = SystemApiUrls.base_prefix + SystemApiUrls.master_update;

        if (null == id) url = SystemApiUrls.base_prefix + SystemApiUrls.master_create;

        String pwd = "";

        if (BaseUtil.isNotBlank(password)) {
            // 密码没有作任何修改
            if (password.length() > 30) {
                // 同时本次是新增操作，则密码自动设置为123456
                if (null == id) pwd = Constant.DEFAULT_USER_PASSWORD;
            } else {
                pwd = password;
            }
        }

        // 保存管理员信息
        Map<String, Object> map = HttpRequestUtil.getCompleteData(url, "id=" + id + "&roleId=" + roleId + "&status=" + status + "&companyId=" + companyId + "&account=" + UnicodeUtil.unicodeEncode(account) + "&name=" + UnicodeUtil.unicodeEncode(name) + "&mobile=" + mobile + "&email=" + UnicodeUtil.unicodeEncode(email) + (BaseUtil.isNotBlank(pwd) ? "&password=" + pwd : ""));

        if (map.get("code").equals("0")) {
            // 根据传回来的管理员信息继续保存角色信息
            return HttpRequestUtil.getCompleteData(SystemApiUrls.base_prefix + SystemApiUrls.role_updateRoleRelation, "masterId=" + map.get("data") + "&roleId=" + roleId);
        } else {
            return map;
        }
    }

    @Override
    public Map<String, Object> deleteById(HttpServletRequest request, Integer id) {
        LogInfoServiceImpl.saveLogInfo(request, "删除管理员信息，ID：" + id);

        return HttpRequestUtil.getCompleteData(SystemApiUrls.base_prefix + SystemApiUrls.master_delete, "id=" + id);
    }

    @Override
    public Map<String, Object> batchDel(HttpServletRequest request, String ids) {
        LogInfoServiceImpl.saveLogInfo(request, "批量删除管理员信息，ID：" + ids);

        return HttpRequestUtil.getCompleteData(SystemApiUrls.base_prefix + SystemApiUrls.master_batchDel, "ids=" + ids);
    }

    @Override
    public Map<String, Object> updateStatus(HttpServletRequest request, Integer id, Integer status) {
        LogInfoServiceImpl.saveLogInfo(request, "修改管理员状态信息，ID：" + id + "，启用状态：" + status);

        return HttpRequestUtil.getCompleteData(SystemApiUrls.base_prefix + SystemApiUrls.master_updateStatus, "id=" + id + "&status=" + status);
    }

    @Override
    public Map<String, Object> modifyMasterPwd(HttpServletRequest request, Integer id) {
        LogInfoServiceImpl.saveLogInfo(request, "重置管理员密码，ID：" + id);

        return HttpRequestUtil.getCompleteData(SystemApiUrls.base_prefix + SystemApiUrls.master_modifyPwd, "id=" + id);
    }
}